﻿
Partial Class WebForm_ManageBulletin_AddServiceBulletin
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.fv.InsertItemTemplate = Me.fv.ItemTemplate
        Me.fv.EditItemTemplate = Me.fv.ItemTemplate
    End Sub
    Protected Sub fv_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles fv.ItemCommand
        If e.CommandName = "btnSave" Then
            If Me.SaveData() = True Then
                RaiseEvent Saved(Me, New EventArgs)
            End If

        End If
    End Sub
    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound
        Me.BindOrganization()
        Me.BindItemType()
    End Sub
    Private Sub BindOrganization()

        Dim ddlOrganization As DropDownList = TryCast(Me.fv.FindControl("ddlOrganization"), DropDownList)

        ddlOrganization.DataTextField = "OrganizationName"
        ddlOrganization.DataValueField = "UOrganizationID"
        ddlOrganization.DataSource = MyObj.GetDDLDirectOrganizationData()
        ddlOrganization.DataBind()

    End Sub
    Private Sub BindItemType()
        Dim ddlItemType As DropDownList = TryCast(Me.fv.FindControl("ddlItemType"), DropDownList)

        ddlItemType.DataTextField = "Text"
        ddlItemType.DataValueField = "Value"
        ddlItemType.DataSource = MyObj.GetCodeMappingData("CleanPlan", "ItemType")
        ddlItemType.DataBind()

    End Sub
    Public Sub InitData()
        Me.fv.ChangeMode(FormViewMode.Insert)
    End Sub
    Public Sub InitData(ByVal ServiceID As String)
        Me.fv.ChangeMode(FormViewMode.Edit)
        Me.ServiceID = ServiceID
        Me.BindData()

    End Sub
    Private Sub BindData()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.AppendFormat("select s.* ")
        sql.AppendFormat(" from ServiceBulletin s ")
        sql.AppendFormat("where 1=1 ")
        sql.AppendFormat("and s.ServiceID = '{0}' ", Me.ServiceID)

        dt = dat.GetData(sql)
        TryCast(Me.fv.FindControl("ddlItemType"), DropDownList).SelectedItem.Text = dt.Rows(0)("ItemType").ToString()
        TryCast(Me.fv.FindControl("ddlOrganization"), DropDownList).SelectedItem.Text = dt.Rows(0)("Organization").ToString()

        Me.fv.DataSource = dt
        Me.fv.DataBind()
    End Sub

    Private Function SaveData() As Boolean
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim cuser As String
        cuser = clsAuth.GetUserID()

        Using scope As New Transactions.TransactionScope
            If Me.fv.CurrentMode = FormViewMode.Insert Then
                sql.Length = 0
                sql.AppendFormat("insert  ServiceBulletin(ServiceSubject,ServiceContent,StartDate,EndDate,PeopleCount,Organization,ItemType,CreateDate,CreateBy,Contact,PhoneNumber,Email) select ")
                sql.AppendFormat("'{0}' ", TryCast(fv.FindControl("txtServiceSubject"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(fv.FindControl("txtServiceContent"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(fv.FindControl("txtStartDate"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(fv.FindControl("txtEndDate"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(fv.FindControl("txtPeopleCount"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(fv.FindControl("ddlOrganization"), DropDownList).SelectedItem.Text)
                sql.AppendFormat(",'{0}' ", TryCast(fv.FindControl("ddlItemType"), DropDownList).SelectedItem.Text)
                sql.AppendFormat(",'{0:yyyy/MM/dd}' ", DateTime.Now)
                sql.AppendFormat(",'{0}' ", cuser)
                sql.AppendFormat(",'{0}' ", TryCast(fv.FindControl("txtContact"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(fv.FindControl("txtPhoneNumber"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(fv.FindControl("txtEmail"), TextBox).Text)
                dat.AccData(sql)
                Me.ServiceSubject = TryCast(fv.FindControl("txtServiceSubject"), TextBox).Text

            ElseIf Me.fv.CurrentMode = FormViewMode.Edit Then
                sql.Length = 0
                sql.AppendFormat("update ServiceBulletin set ")
                sql.AppendFormat(" ServiceSubject='{0}' ", TryCast(fv.FindControl("txtServiceSubject"), TextBox).Text)
                sql.AppendFormat(",ServiceContent='{0}' ", TryCast(fv.FindControl("txtServiceContent"), TextBox).Text)
                sql.AppendFormat(",StartDate='{0}' ", TryCast(fv.FindControl("txtStartDate"), TextBox).Text)
                sql.AppendFormat(",EndDate='{0}' ", TryCast(fv.FindControl("txtEndDate"), TextBox).Text)
                sql.AppendFormat(",PeopleCount='{0}' ", TryCast(fv.FindControl("txtPeopleCount"), TextBox).Text)
                sql.AppendFormat(",Organization='{0}' ", TryCast(fv.FindControl("ddlOrganization"), DropDownList).SelectedItem.Text)
                sql.AppendFormat(",ItemType='{0}' ", TryCast(fv.FindControl("ddlItemType"), DropDownList).SelectedItem.Text)
                sql.AppendFormat(",UpdateDate={0} ", "getdate()")
                sql.AppendFormat(",UpdateBy='{0}' ", cuser)
                sql.AppendFormat(",Contact='{0}' ", TryCast(fv.FindControl("txtContact"), TextBox).Text)
                sql.AppendFormat(",PhoneNumber='{0}' ", TryCast(fv.FindControl("txtPhoneNumber"), TextBox).Text)
                sql.AppendFormat(",Email='{0}' ", TryCast(fv.FindControl("txtEmail"), TextBox).Text)

                sql.AppendFormat(" where ServiceID = '{0}' ", Me.ServiceID)
                dat.AccData(sql)
                Me.ServiceSubject = TryCast(fv.FindControl("txtServiceSubject"), TextBox).Text

            End If

            scope.Complete()
        End Using

        Return True
    End Function
    Public Property ServiceID() As Integer
        Get
            Return ViewState("ServiceID")
        End Get
        Set(ByVal value As Integer)
            ViewState("ServiceID") = value
        End Set
    End Property
    Public Property ServiceSubject() As String
        Get
            Return ViewState("ServiceSubject")
        End Get
        Set(ByVal value As String)
            ViewState("ServiceSubject") = value
        End Set
    End Property
    Public Event Saved(ByVal source As Object, ByVal e As EventArgs)

End Class
